package com.macro.mall.tiny.modules.pms.service.impl;

import com.macro.mall.tiny.common.service.RedisService;
import com.macro.mall.tiny.modules.pms.model.PmsUserAction;
import com.macro.mall.tiny.modules.pms.mapper.PmsUserActionMapper;
import com.macro.mall.tiny.modules.pms.service.PmsErrorCaseService;
import com.macro.mall.tiny.modules.pms.service.PmsUserActionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.macro.mall.tiny.security.util.MemberContext;
import io.swagger.models.auth.In;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户行为表 服务实现类
 * </p>
 *
 * @author tyrone
 * @since 2024-10-09
 */
@Service
public class PmsUserActionServiceImpl extends ServiceImpl<PmsUserActionMapper, PmsUserAction> implements PmsUserActionService {

    @Autowired
    private RedisService redisService;

    public static final String REDIS_KEY_PREFIX = "pms:user:action";

    public static final String REDIS_KEY_DETAIL_PREFIX = "pms:user:action_detail";

    @Autowired
    private PmsErrorCaseService pmsErrorCaseService;

    @Override
    public void handle(Integer actionId, Integer type, Integer id) {
        if (actionId == 0) {
            redisService.getRedisTemplate()
                    .opsForSet().add(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":like:" + MemberContext.getMember().getId(), id);

            pmsErrorCaseService.getErrorCaseCategory();
            redisService.incr(REDIS_KEY_PREFIX + ":" + type + ":like:" + id, 1L);
        } else if (actionId == 1) {
            Long removedNum = redisService.getRedisTemplate()
                    .opsForSet().remove(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":like:" + MemberContext.getMember().getId(), id);
            if (removedNum != null && removedNum > 0)
                redisService.decr(REDIS_KEY_PREFIX + ":" + type + ":like:" + id, 1L);
        } else if (actionId == 2) {
            redisService.getRedisTemplate()
                    .opsForSet().add(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":collect:" + MemberContext.getMember().getId(), id);
            redisService.incr(REDIS_KEY_PREFIX + ":" + type + ":collect:" + id, 1L);
        } else if (actionId == 3) {
            Long removedNum = redisService.getRedisTemplate()
                    .opsForSet().remove(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":collect:" + MemberContext.getMember().getId(), id);
            if (removedNum != null && removedNum > 0)
                redisService.decr(REDIS_KEY_PREFIX + ":" + type + ":collect:" + id, 1L);
        }
    }

    @Override
    public List<Object> listItem(Integer type, Integer actionId) {
        String actionStr = null;
        if (actionId == 0) {
            actionStr = "like";
        } else {
            actionStr = "collect";
        }
        Set<Object> members = redisService.getRedisTemplate().opsForSet().members(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":" + actionStr + ":" + MemberContext.getMember().getId());
        if (members != null && !members.isEmpty()) return new ArrayList<>(members);
        return Collections.emptyList();
    }

    public static enum ActionTarget {
        ERROR_CODE(0, "错误码"),
        ERROR_CASE(1, "故障现象");

        ActionTarget(Integer type, String value) {
            this.value = value;
            this.type = type;
        }

        String value;
        Integer type;
    }
}
